package com.tangkun.environment.fragment.main;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.tangkun.environment.R;
import com.tangkun.environment.application.Session;
import com.tangkun.environment.entity.token.LoginReturn;
import com.tangkun.environment.fragment.BaseFragment;
import com.tangkun.environment.view.CircleProgress;

import org.simple.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 工作子Fragment 已登录且为企业主身份
 */

public class Main2ItemFragment extends BaseFragment {

    @BindView(R.id.circleprogress)
    CircleProgress circleprogress;
    @BindView(R.id.tv_main_wancheng_percent)
    TextView tvMainWanchengPercent;
    @BindView(R.id.tv_main_wancheng_status)
    TextView tvMainWanchengStatus;
    @BindView(R.id.tv_main_jiedao)
    TextView tvMainJiedao;
    @BindView(R.id.ll_main_jiedao)
    LinearLayout llMainJiedao;
    @BindView(R.id.tv_main_shequ)
    TextView tvMainShequ;
    @BindView(R.id.ll_main_shequ)
    LinearLayout llMainShequ;
    @BindView(R.id.tv_main_weizhi)
    TextView tvMainWeizhi;
    @BindView(R.id.ll_main_weizhi)
    LinearLayout llMainWeizhi;
    @BindView(R.id.ll_main_fuzeren)
    LinearLayout llMainFuzeren;
    @BindView(R.id.tv_main_wuye)
    TextView tvMainWuye;
    @BindView(R.id.ll_main_wuye)
    LinearLayout llMainWuye;
    @BindView(R.id.tv_main_mingcheng)
    TextView tvMainMingcheng;
    @BindView(R.id.ll_main_mingcheng)
    LinearLayout llMainMingcheng;
    @BindView(R.id.tv_main_fuzeren_name)
    TextView tvMainFuzerenName;
    @BindView(R.id.tv_main_fuzeren_tel)
    TextView tvMainFuzerenTel;

    /**
     * 数据源
     */
    private List<LoginReturn.UserInfo.BindlistBean> dataList = new ArrayList<LoginReturn.UserInfo.BindlistBean>();
    /**
     * 数据的索引
     */
    private int index;

    private Random mRandom;


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        super.onCreateView(inflater, container, savedInstanceState);
        index = getArguments().getInt("index");
        dataList = Session.getInstance().getUserBindlist();
        View view = inflater.inflate(R.layout.fragment_main2_item, container, false);
        ButterKnife.bind(this, view);
        EventBus.getDefault().register(this);
        initData();
        mRandom = new Random();
        return view;
    }

    private void initData() {
        LoginReturn.UserInfo.BindlistBean bean = dataList.get(index);
        circleprogress.reset();
        circleprogress.clearAnimation();//清除动画试试
        circleprogress.setValue(100); //值
        //完成情况
        if (bean.finishnum < bean.totalnum) {
            tvMainWanchengPercent.setText("本月数据\n" + "未提交");
        } else if (bean.finishnum == bean.totalnum) {
            tvMainWanchengPercent.setText("本月数据\n" + "已提交");
        }
        //完成情况 这里啥都不显示
        tvMainWanchengStatus.setText("");
        //名称
        if (!TextUtils.isEmpty(bean.name)) {
            tvMainMingcheng.setText(bean.name);
        } else {
            tvMainMingcheng.setText("");
        }
        //街道
        if (!TextUtils.isEmpty(bean.streetname)) {
            tvMainJiedao.setText(bean.streetname);
        } else {
            tvMainJiedao.setText("");
        }
        //社区
        if (!TextUtils.isEmpty(bean.communityname)) {
            tvMainShequ.setText(bean.communityname);
        } else {
            tvMainShequ.setText("");
        }
        //隐藏社区
        llMainShequ.setVisibility(View.GONE);
        //位置
        if (!TextUtils.isEmpty(bean.address)) {
            tvMainWeizhi.setText(bean.address);
        } else {
            tvMainWeizhi.setText("");
        }
        //负责人名字
        if (!TextUtils.isEmpty(bean.legalname)) {
            tvMainFuzerenName.setText(bean.legalname);
            tvMainFuzerenName.setVisibility(View.VISIBLE);
        } else {
            tvMainFuzerenName.setText("");
            tvMainFuzerenName.setVisibility(View.GONE);
        }
        //负责人电话
        if (!TextUtils.isEmpty(bean.legaltel)) {
            tvMainFuzerenTel.setText(bean.legaltel);
        } else {
            tvMainFuzerenTel.setText("");
        }
        //物业
        llMainWuye.setVisibility(View.GONE);
    }

    @Override
    public void onResume() {
        super.onResume();
    }

    @OnClick({R.id.ll_main_fuzeren})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.ll_main_fuzeren://负责人
                call();
                break;
        }
    }

    /**
     * 打电话
     */
    private void call() {
        if (!TextUtils.isEmpty(tvMainFuzerenTel.getText().toString())) {
            Intent intent = new Intent(Intent.ACTION_DIAL);
            Uri data = Uri.parse("tel:" + tvMainFuzerenTel.getText().toString());
            intent.setData(data);
            startActivity(intent);
        } else {
            showToast("暂无负责人电话");
        }
    }
}
